package com.uubx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uubx.exception.CustomerException;
import com.uubx.pojo.Customer;
import com.uubx.pojo.Insurance;
import com.uubx.result.MessageCode;
import com.uubx.result.Result;
import com.uubx.result.ResultUtils;
import com.uubx.service.InsuranceService;
import com.uubx.vo.InsuranceVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author wyy
 * @Date 2020/12/30 23:26
 * @Version 1.0
 */
@RequestMapping("/insurance")
@Api(description = "保险相关的接口")
@ResponseBody
@Controller
public class InsuranceController {

    @Autowired
    private InsuranceService insuranceService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "保险添加接口",notes = "保险添加接口",httpMethod = "POST")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功! data代表返回的数据"),
            @ApiResponse(code=500,message="后台逻辑错误!"),
            @ApiResponse(code=404,message="找不到页面!"),
            @ApiResponse(code=405,message="请求方式不正确，比如后台接收是post，但前端请求的是get!"),
            @ApiResponse(code=406,message="页面地址和接口名冲突!"),
            @ApiResponse(code=415,message="请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串!")
    })
    public Result add(@Validated @ModelAttribute Insurance insurance, String desc){

        insuranceService.insertInsurance(insurance,desc);
        return ResultUtils.buildSuccess();
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(value = "保险修改接口",notes = "保险修改接口",httpMethod = "POST")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功! data代表返回的数据"),
            @ApiResponse(code=500,message="后台逻辑错误!"),
            @ApiResponse(code=404,message="找不到页面!"),
            @ApiResponse(code=405,message="请求方式不正确，比如后台接收是post，但前端请求的是get!"),
            @ApiResponse(code=406,message="页面地址和接口名冲突!"),
            @ApiResponse(code=415,message="请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串!")
    })
    public Result update(Insurance insurance,String desc){

        insuranceService.updateInsurance(insurance,desc);

        return ResultUtils.buildSuccess();
    }
    @RequestMapping(value = "/updateStatus",method = RequestMethod.POST)
    @ApiOperation(value = "保险修改状态接口",notes = "保险修改状态接口",httpMethod = "POST")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功! data代表返回的数据"),
            @ApiResponse(code=500,message="后台逻辑错误!"),
            @ApiResponse(code=404,message="找不到页面!"),
            @ApiResponse(code=405,message="请求方式不正确，比如后台接收是post，但前端请求的是get!"),
            @ApiResponse(code=406,message="页面地址和接口名冲突!"),
            @ApiResponse(code=415,message="请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串!")
    })
    public Result updateStatus(Long insuranceId,Integer status){
        //前端乱传状态  1为上架 2为下架 3为逻辑删除
        if (status<1||status>3){
            return ResultUtils.buildFail(MessageCode.INSURANCE_STATUS);
        }
        Insurance insurance=new Insurance();
        insurance.setId(insuranceId);
        insurance.setStatus(status);
        insuranceService.updateInsuranceStatus(insurance);
        return ResultUtils.buildSuccess();
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询保险详情",notes = "根据id查询保险详情",httpMethod = "GET")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功! data代表返回的数据"),
            @ApiResponse(code=500,message="后台逻辑错误!"),
            @ApiResponse(code=404,message="找不到页面!"),
            @ApiResponse(code=405,message="请求方式不正确，比如后台接收是post，但前端请求的是get!"),
            @ApiResponse(code=406,message="页面地址和接口名冲突!"),
            @ApiResponse(code=415,message="请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串!")
    })
    public Result findInsuranceById(@PathVariable("id") Long id){

        Insurance insuranceById = insuranceService.findInsuranceById(id);

        return ResultUtils.buildSuccess(insuranceById);
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation(value = "条件分页查询",notes = "条件分页查询",httpMethod = "GET")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功! data代表返回的数据"),
            @ApiResponse(code=500,message="后台逻辑错误!"),
            @ApiResponse(code=404,message="找不到页面!"),
            @ApiResponse(code=405,message="请求方式不正确，比如后台接收是post，但前端请求的是get!"),
            @ApiResponse(code=406,message="页面地址和接口名冲突!"),
            @ApiResponse(code=415,message="请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串!")
    })
    public Result list(InsuranceVo insuranceVo) throws CustomerException {

        PageHelper.offsetPage(insuranceVo.getStartIndex(),insuranceVo.getPageSize());
        List<Map> insuranceWhere = insuranceService.findInsuranceWhere(insuranceVo);


        PageInfo<Map> mapPageInfo = new PageInfo<>(insuranceWhere);

        if (mapPageInfo.getList().isEmpty()){
            throw new CustomerException(MessageCode.INSURANCE_EMPTY);
        }

        return ResultUtils.buildSuccess(mapPageInfo);
    }

    @RequestMapping(value = "/desc/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "查看商品详情",notes = "查看商品详情",httpMethod = "GET")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功! data代表返回的数据"),
            @ApiResponse(code=500,message="后台逻辑错误!"),
            @ApiResponse(code=404,message="找不到页面!"),
            @ApiResponse(code=405,message="请求方式不正确，比如后台接收是post，但前端请求的是get!"),
            @ApiResponse(code=406,message="页面地址和接口名冲突!"),
            @ApiResponse(code=415,message="请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串!")
    })
    public Result findInsuranceDescById(@PathVariable("id") Long id) throws CustomerException {
        String desc = insuranceService.findInsuranceDescById(id);

        return ResultUtils.buildSuccess(desc);
    }
}
